{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7.1 \u53ef\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u53c2\u6570\u7684\u51fd\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u6784\u9020\u4e00\u4e2a\u53ef\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u53c2\u6570\u7684\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u80fd\u8ba9\u4e00\u4e2a\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a*\u53c2\u6570\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def avg(first, *rest):\n    return (first + sum(rest)) / (1 + len(rest))\n\n# Sample use\navg(1, 2) # 1.5\navg(1, 2, 3, 4) # 2.5"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crest\u662f\u7531\u6240\u6709\u5176\u4ed6\u4f4d\u7f6e\u53c2\u6570\u7ec4\u6210\u7684\u5143\u7ec4\u3002\u7136\u540e\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u628a\u5b83\u5f53\u6210\u4e86\u4e00\u4e2a\u5e8f\u5217\u6765\u8fdb\u884c\u540e\u7eed\u7684\u8ba1\u7b97\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u4f7f\u7528\u4e00\u4e2a\u4ee5**\u5f00\u5934\u7684\u53c2\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import html\n\ndef make_element(name, value, **attrs):\n    keyvals = [' %s=\"%s\"' % item for item in attrs.items()]\n    attr_str = ''.join(keyvals)\n    element = '<{name}{attrs}>{value}</{name}>'.format(\n                name=name,\n                attrs=attr_str,\n                value=html.escape(value))\n    return element\n\n# Example\n# Creates '<item size=\"large\" quantity=\"6\">Albatross</item>'\nmake_element('item', 'Albatross', size='large', quantity=6)\n\n# Creates '<p>&lt;spam&gt;</p>'\nmake_element('p', '<spam>')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u91cc\uff0cattrs\u662f\u4e00\u4e2a\u5305\u542b\u6240\u6709\u88ab\u4f20\u5165\u8fdb\u6765\u7684\u5173\u952e\u5b57\u53c2\u6570\u7684\u5b57\u5178\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8fd8\u5e0c\u671b\u67d0\u4e2a\u51fd\u6570\u80fd\u540c\u65f6\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\uff0c\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528*\u548c**\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def anyargs(*args, **kwargs):\n    print(args) # A tuple\n    print(kwargs) # A dict"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u6240\u6709\u4f4d\u7f6e\u53c2\u6570\u4f1a\u88ab\u653e\u5230args\u5143\u7ec4\u4e2d\uff0c\u6240\u6709\u5173\u952e\u5b57\u53c2\u6570\u4f1a\u88ab\u653e\u5230\u5b57\u5178kwargs\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a*\u53c2\u6570\u53ea\u80fd\u51fa\u73b0\u5728\u51fd\u6570\u5b9a\u4e49\u4e2d\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570\u540e\u9762\uff0c\u800c **\u53c2\u6570\u53ea\u80fd\u51fa\u73b0\u5728\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u3002\n\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728*\u53c2\u6570\u540e\u9762\u4ecd\u7136\u53ef\u4ee5\u5b9a\u4e49\u5176\u4ed6\u53c2\u6570\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def a(x, *args, y):\n    pass\n\ndef b(x, *args, y, **kwargs):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u53c2\u6570\u5c31\u662f\u6211\u4eec\u6240\u8bf4\u7684\u5f3a\u5236\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5728\u540e\u97627.2\u5c0f\u8282\u8fd8\u4f1a\u8be6\u7ec6\u8bb2\u89e3\u5230\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}